//
//  ZNewTodoItemVC.m
//  WeClubs
//
//  Created by Zed Link on 17/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZNewTodoItemVC.h"
#import <Masonry.h>

@interface ZNewTodoItemVC ()

@end

@implementation ZNewTodoItemVC

- (void)zLoadSubview
{
    [super zLoadSubview];
    
    UITableView *mainView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    mainView.delegate = self;
    mainView.dataSource = self;
//    mainView.bounces = NO;
    mainView.contentInset = UIEdgeInsetsMake(0, 0, 36, 0);
    mainView.separatorColor = SEPARATOR_BG_COLOR;
    mainView.estimatedRowHeight = 44;
    mainView.rowHeight = UITableViewAutomaticDimension;
    
    [mainView registerNib:[UINib nibWithNibName:@"ZNewTextViewCell" bundle:nil] forCellReuseIdentifier:NewTextViewCellReuseID];
    [mainView registerNib:[UINib nibWithNibName:@"ZNewMemberChoseCell" bundle:nil] forCellReuseIdentifier:NewMemberChoseCellReuseID];
    [mainView registerNib:[UINib nibWithNibName:@"ZNewClubCell" bundle:nil] forCellReuseIdentifier:NewClubCellReuseID];
    [mainView registerNib:[UINib nibWithNibName:@"ZNewMemberGroupCell" bundle:nil] forCellReuseIdentifier:NewMemberGroupCellReuseID];
    [mainView registerNib:[UINib nibWithNibName:@"ZNewTextFieldCell" bundle:nil] forCellReuseIdentifier:NewTextFieldCellReuseID];
    [mainView registerNib:[UINib nibWithNibName:@"ZNewSwitchCell" bundle:nil] forCellReuseIdentifier:NewSwitchCellReuseID];
    
    [mainView registerClass:[ZCommentHeader class] forHeaderFooterViewReuseIdentifier:TodoHeaderReuseID];
    
    self.mainView = mainView;
    [self.view addSubview:mainView];
    [mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(64, 0, 0, 0));
    }];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.interfaceConfig.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *sectionDict = self.interfaceConfig[section];
    NSArray *dataArr = sectionDict[NewDataKey];
    
    return dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    NSDictionary *sectionConfig = self.interfaceConfig[section];
    NSString *headerTitle = sectionConfig[NewHeaderKey];
    
    return headerTitle == nil ? 0.1 : 34;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSDictionary *sectionConfig = self.interfaceConfig[section];
    NSString *headerTitle = sectionConfig[NewHeaderKey];
    
    ZCommentHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:TodoHeaderReuseID];
    header.style = ZTableHeaderStyleDetail;
    header.alignment = ZTableHeaderAlignmentTop;
    [header zSetTitle:headerTitle];
    
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section + 1 < self.interfaceConfig.count) {
        NSDictionary *nextSectionConfig = self.interfaceConfig[section + 1];
        NSString *nextHeaderTitle = nextSectionConfig[NewHeaderKey];
        return nextHeaderTitle == nil ? 12 : 0.1;
    } ;
    return 12;
}

#pragma mark - Getter & Setter

- (NSMutableArray *)interfaceConfig
{
    if (_interfaceConfig == nil) {
        NSURL *configURL = [[NSBundle mainBundle] URLForResource:@"ZUserInterfaceConfig.plist" withExtension:nil];
        NSDictionary *rootDict = [NSDictionary dictionaryWithContentsOfURL:configURL];
        NSArray *interfaceConfig = [rootDict objectForKey:NSStringFromClass(self.class)];
        
        _interfaceConfig = [NSMutableArray arrayWithCapacity:interfaceConfig.count];
        for (NSDictionary *dict in interfaceConfig) {
            NSMutableDictionary *dictM = [NSMutableDictionary dictionaryWithDictionary:dict];
            [_interfaceConfig addObject:dictM];
        }
    }
    return _interfaceConfig;
}

@end
